package com.mvp.method2.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hhx.baselibrary.bean.BaseResponse;
import com.hhx.test_library.R;
import com.hhx.test_library.bean.Login;
import com.mvp.method2.base.MVPActivity;
import com.mvp.method2.imvp.login.LoginContract;
import com.mvp.method2.imvp.login.LoginPresenter;
import com.mvp.method2.retrofit.APIServiceManager;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Login2MVPActivity extends MVPActivity<LoginPresenter> implements LoginContract.View {

    private Button btnLogin, btnRetrofit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_login2_mvp);
        super.onCreate(savedInstanceState);
        btnLogin = findViewById(R.id.button0_login);
        btnRetrofit = findViewById(R.id.button_retrofit_login);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button0_login: //Login测试2(MVP+Retrofit+Rxjava)
                HashMap<String, Object> map = new HashMap<>();
                map.put("phone", "18245083860");
                map.put("password", "13156");
                mPresenter.login(map);
                break;
            case R.id.button_retrofit_login://普通写法（Retrofit)
                loginByRetrofit();
                break;
        }

    }

    private void loginByRetrofit() {
        showProgressDialog();
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", "18245083860");
        map.put("password", "13156");
        Call<BaseResponse<Login>> call = APIServiceManager.getInstance().getApiService().loginCall(map);
        call.enqueue(new Callback<BaseResponse<Login>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<Login>> call, Response<BaseResponse<Login>> response) {
                if (response.isSuccessful()) {
                    refreshSuccess(response.body());
                }
                dissmissProgressDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<Login>> call, Throwable t) {
                refreshFailed(t.getMessage());
                dissmissProgressDialog();
            }
        });
        addCalls(call);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void refreshSuccess(BaseResponse<Login> baseResponse) {
        if (baseResponse.getCode() == 1) {
            Toast.makeText(this, baseResponse.getMsg(), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, baseResponse.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void refreshFailed(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}
